home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / UPC12BS1.ZIP / UUCICO / SCRIPT.C < prev    next >
C/C++ Source or Header  |  1993-10-03  |  17KB  |  494 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    s c r i p t . c                                                 */
  3. /*                                                                    */
  4. /*    Script processing routines for UUPC/extended                    */
  5. /*                                                                    */
  6. /*    John H. DuBois III  3/31/90                                     */
  7. /*--------------------------------------------------------------------*/
  8.  
  9. /*--------------------------------------------------------------------*/
  10. /*                        System include files                        */
  11. /*--------------------------------------------------------------------*/
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <ctype.h>
  16. #include <string.h>
  17. #include <time.h>
  18.  
  19. /*--------------------------------------------------------------------*/
  20. /*                    UUPC/extended include files                     */
  21. /*--------------------------------------------------------------------*/
  22.  
  23. #include "lib.h"
  24. #include "dcp.h"
  25. #include "dcpsys.h"
  26. #include "hostable.h"
  27. #include "hlib.h"
  28. #include "modem.h"
  29. #include "script.h"
  30. #include "security.h"
  31. #include "ssleep.h"
  32. #include "catcher.h"
  33. #include "usrcatch.h"
  34. #include "commlib.h"
  35.  
  36. /*--------------------------------------------------------------------*/
  37. /*                           Local defines                            */
  38. /*--------------------------------------------------------------------*/
  39.  
  40. #define MAXMATCH 64              /* max length of search string; must
  41.                                     be a power of 2                  */
  42. #define QINDMASK (MAXMATCH - 1)  /* bit mask to get queue index      */
  43.  
  44. #define EOTMSG "\004\r\004\r"
  45.  
  46. /*--------------------------------------------------------------------*/
  47. /*                    Internal function prototypes                    */
  48. /*--------------------------------------------------------------------*/
  49.  
  50. static int StrMatch(char *MatchStr, char C, char **failure);
  51.                                  /* Internal match routine           */
  52.  
  53. static boolean Match( char *Search,
  54.                       char *Buffer,
  55.                       size_t *SearchPos);
  56.  
  57. static size_t MatchInit( const char *MatchStr );
  58.  
  59. static void writestr(register char *s);
  60.  
  61. static void flushScriptBuffer( void );
  62.  
  63. static void slowWrite( char *s, size_t len);
  64.  
  65. /*--------------------------------------------------------------------*/
  66. /*                          Global variables                          */
  67. /*--------------------------------------------------------------------*/
  68.  
  69. currentfile();
  70.  
  71. static char scriptBuffer[40];    // Can be shorter then longest send
  72.                                  // string, as longer strings are
  73.                                  // send without buffering
  74.  
  75. static size_t scriptBufferLen = 0;
  76.  
  77. /*--------------------------------------------------------------------*/
  78. /*       e x p e c t s t r                                            */
  79. /*                                                                    */
  80. /*       wait for a pattern on input                                  */
  81. /*                                                                    */
  82. /*       expectstr reads characters from input using sread, and       */
  83. /*       compares them to a search string.  It reads characters       */
  84. /*       until either the search string has been seen on the input    */
  85. /*       or a specified timeout interval has passed without any       */
  86. /*       characters being read.                                       */
  87. /*                                                                    */
  88. /*      Global variables: none.                                       */
  89. /*                                                                    */
  90. /*      Input parameters:                                             */
  91. /*      Search is the string that is searched for on the input.       */
  92. /*      Timeout is the timeout interval passed to sread.              */
  93. /*                                                                    */
  94. /*      Output parameters: none.                                      */
  95. /*                                                                    */
  96. /*      Return value:                                                 */
  97. /*      TRUE is returned if the search string is found on input.      */
  98. /*      FALSE is returned if sread times out.                         */
  99. /*--------------------------------------------------------------------*/
  100.  
  101. int expectstr(char *Search, unsigned int Timeout, char **failure)
  102. {
  103.    char buf[BUFSIZ];
  104.    int result;
  105.    time_t quit = time( NULL ) + Timeout;
  106.    register char *ptr = buf;
  107.  
  108.    printmsg(2, "wanted \"%s\"", Search);
  109.  
  110.    if (!strlen(Search))                      /* expects nothing */
  111.        return TRUE;
  112.  
  113.    StrMatch(Search,'\0', failure);    /* set up search string */
  114.  
  115.    do {
  116.       if (ptr == &buf[BUFSIZ-1])
  117.         ptr = buf;          /* Save last character for term \0  */
  118.  
  119.       if (sread(ptr , 1, (int) (quit - time(NULL))) < 1)
  120.       {
  121.                               /* The scan failed?                    */
  122.          char *s;
  123.  
  124.          if ( terminate_processing || raised )
  125.             return 0;
  126.  
  127.          while ( ptr > buf )
  128.             if (*(--ptr) > ' ')
  129.                break;    /* Locate the last printable char      */
  130.  
  131.          *(ptr+1) = '\0';   /* Terminate the string             */
  132.  
  133.          for ( s = buf; (*s > '\0') && (*s <= ' '); s++ );
  134.                             /* Locate the first printable char  */
  135.  
  136.          while ( ptr-- > s )/* Zap control chars                */
  137.             if (*ptr < ' ')
  138.                *ptr = '?';
  139.  
  140.          if ( debuglevel < 2 )
  141.             printmsg(1, "wanted \"%s\"", Search);
  142.  
  143.          printmsg(1, "got ??? \"%s\"",s );
  144.          return FALSE;
  145.  
  146.       } /* if (sread(ptr , 1, Timeout) < 1) */
  147.  
  148.       *ptr &= 0x7f;
  149.  
  150.       result = StrMatch(Search, *ptr++, failure);
  151.    } while (!result);
  152.  
  153.    return result;
  154.  
  155. } /*expectstr*/
  156.  
  157. /*
  158.  *      StrMatch: Incrementally search for a string.
  159.  *      John H. DuBois III  3/31/90
  160.  *      StrMatch searches for a string in a sequence of characters.
  161.  *      The string to search for is passed in an initial setup call
  162.  *      (input character in this call is \0)
  163.  *      Further calls with the search string pass one
  164.  *      character per call.
  165.  *      The characters are built up into an input string.
  166.  *      After each character is added to the input string,
  167.  *      the search string is compared to the last length(search string)
  168.  *      characters of the input string to determine whether the search
  169.  *      string has been found.
  170.  *
  171.  *      Global variables: none.
  172.  *
  173.  *      Input parameters:
  174.  *      MatchStr is the string to search for.
  175.  *      C is the character to add to the input string.
  176.  *      It is ignored on a setup call.
  177.  *
  178.  *      Output parameters: None.
  179.  *
  180.  *      Return value:
  181.  *      On the setup call, -1 is returned if the search string is
  182.  *      longer than the input string buffer.  Otherwise, 0 is returned.
  183.  *
  184.  *      On comparison calls,
  185.  *          1 is returned if the search string has been found.
  186.  *          > 1 is returned if a failure string has been found.
  187.  *          Otherwise 0 is returned.
  188.  */
  189.  
  190. static int StrMatch(char *MatchStr, char C, char **failure)
  191. {
  192. /*
  193.  *      The input string is stored in a circular buffer of MAXMATCH
  194.  *      characters.  If the search string is found in the input,
  195.  *      then the last character added to the buffer will be the last
  196.  *      character of the search string.  Therefore, the string
  197.  *      compare will always start SearchLen characters behind the
  198.  *      position where characters are added to the buffer.
  199.  */
  200.  
  201.    static char Buffer[MAXMATCH];       /* Input string buffer */
  202.    static size_t PutPos;               /* Where to add chars to buffer */
  203.  
  204.    static size_t SearchPos[MAXLIST];
  205.    static size_t SearchPosition;
  206.